package com.mbc.rmt.manager;

import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRResultSetDataSource;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.util.JRLoader;

import com.mbc.common.CommonSingleton;
import com.mbc.common.dao.CommonDAOFactory;
import com.mbc.common.dao.CurrencyRateDAO;
import com.mbc.common.dao.CurrencyRateDetailDAO;
import com.mbc.common.dao.LogDAO;
import com.mbc.common.entities.CurrencyRate;
import com.mbc.common.entities.CurrencyRateDetail;
import com.mbc.common.entities.Log;
import com.mbc.common.exception.DataDuplicationException;
import com.mbc.common.manager.AbstractMgr;
import com.mbc.common.param.CriteriaRMParams;
import com.mbc.common.utils.KeyGenerator;
import com.mbc.rmt.dao.AgentBalanceDAO;
import com.mbc.rmt.dao.BankDAO;
import com.mbc.rmt.dao.General_COADAO;
import com.mbc.rmt.dao.RemittanceDAOFactory;
import com.mbc.rmt.dao.SavingDAO;
import com.mbc.rmt.data.SavingDataList;
import com.mbc.rmt.entities.AgentBalance;
import com.mbc.rmt.entities.Bank;
import com.mbc.rmt.entities.GeneralLedger;
import com.mbc.rmt.entities.General_COA;
import com.mbc.rmt.entities.Saving;
import com.mbc.rmt.entities.SavingBalanceRpt;
import com.mbc.rmt.entities.SavingLedgerRpt;
import com.mbc.rmt.entities.SavingRpt;

public class SavingMgr extends AbstractMgr{
	
	private SavingDAO savingDao;
	private AgentBalanceDAO agentbalanceDAO;
	private CurrencyRateDetailDAO currencyrateDetailDAO;
	private CurrencyRateDAO currencyRateDAO;
	private General_COADAO generalCOADAO;
	private BankDAO bankDAO;
	private LogDAO logDAO;
	
	public static enum GL_STATUS { CREATE, DELETE }
	
	public static enum STATUS { ADD, UPDATE, DELETE }
	public SavingMgr() {
	}

	public SavingMgr(String sessionId) {
		this.sessionId = sessionId;
	}

	public String create(Saving entity) throws Exception {
		openConnection();
		try {
			beginTransaction();
			savingDao = RemittanceDAOFactory.getSavingDAO(dbManager
					.getConnection());
			currencyrateDetailDAO = CommonDAOFactory.getCurrencyRateDetailDAO(dbManager.getConnection());
			logDAO = CommonDAOFactory.getLogDAO(getConnection());
			Log l_logentity = new Log();
			
			String voucher = "";
			if(entity.getSavingid() == 0){
				Saving l_info = new Saving();
				l_info = savingDao.find(entity.getSavingid());			
				if (l_info != null) {
					throw new Exception("Invoice ID already exists!");
				} 
				else 
				{					
					GoToAgentBalance(STATUS.ADD, entity);
					
					CurrencyRateDetail l_currencyrateDetail = new CurrencyRateDetail();					
					l_currencyrateDetail = currencyrateDetailDAO.findLatest(entity.getRateid());
					
					if (l_currencyrateDetail.getRate()  !=  entity.getExchangerate()){					  
				   
					   l_currencyrateDetail.setCurrencyRateDetailId(KeyGenerator.generateKey(getConnection()));
					   l_currencyrateDetail.setCurrencyRateId(entity.getRateid());
					   l_currencyrateDetail.setRate(entity.getExchangerate());
					   l_currencyrateDetail.seteDateTime(entity.getSavingdate());
					   currencyrateDetailDAO.create(l_currencyrateDetail);
					   
					   l_logentity.setTablename("CurrencyRateDetail");
					   l_logentity.setLogkey(KeyGenerator.generateKey(getConnection()));
					   l_logentity.setKey(l_currencyrateDetail.getCurrencyRateDetailId());
					   l_logentity.setLogstatus(Log.__SAVE_DATA);
					   logDAO.getData(l_logentity);
					}
					
					entity.setSavingid(KeyGenerator.generateKey(getConnection()));
					if(entity.getTransactiontype() == 1)
						voucher = RemittanceSerialMgr.getFormattedSerial(RemittanceSerialMgr.SAVING, entity.getSavingdate(), entity.getBranchId(), getConnection());
					else if(entity.getTransactiontype() == 0)
						voucher = RemittanceSerialMgr.getFormattedSerial(RemittanceSerialMgr.SAVING_RETURN, entity.getSavingdate(), entity.getBranchId(), getConnection());
					
					entity.setSavingno(voucher);
					
					gotoGL(entity, GL_STATUS.CREATE);
					savingDao.create(entity);
					
					l_logentity.setTablename("Saving");
					l_logentity.setLogkey(KeyGenerator.generateKey(getConnection()));
					l_logentity.setKey(entity.getSavingid());
					l_logentity.setUserid(entity.getCreateduser());
					l_logentity.setLogstatus(Log.__SAVE_DATA);
					logDAO.getData(l_logentity);
				}
			}
			else{
				GoToAgentBalance(STATUS.UPDATE, entity);
				
				CurrencyRateDetail l_currencyrateDetail = new CurrencyRateDetail();					
				l_currencyrateDetail = currencyrateDetailDAO.findLatest(entity.getRateid());
				
				if (l_currencyrateDetail.getRate()  !=  entity.getExchangerate()){					  
			   
				   l_currencyrateDetail.setCurrencyRateDetailId(KeyGenerator.generateKey(getConnection()));
				   l_currencyrateDetail.setCurrencyRateId(entity.getRateid());
				   l_currencyrateDetail.setRate(entity.getExchangerate());
				   l_currencyrateDetail.seteDateTime(entity.getSavingdate());
				   currencyrateDetailDAO.create(l_currencyrateDetail);
				   
				   l_logentity.setTablename("CurrencyRateDetail");
					l_logentity.setLogkey(KeyGenerator.generateKey(getConnection()));
					l_logentity.setKey(l_currencyrateDetail.getCurrencyRateDetailId());
					l_logentity.setLogstatus(Log.__SAVE_DATA);
					logDAO.getData(l_logentity);
				}
				
				savingDao = RemittanceDAOFactory.getSavingDAO(dbManager.getConnection());
				gotoGL(entity, GL_STATUS.CREATE);
				savingDao.update(entity);
				
				l_logentity.setTablename("Saving");
				l_logentity.setLogkey(KeyGenerator.generateKey(getConnection()));
				l_logentity.setKey(entity.getSavingid());
				l_logentity.setUserid(entity.getModifieduser());
				l_logentity.setLogstatus(Log.__UPDATE_DATA);
				logDAO.getData(l_logentity);
			}
			commintTransaction();
			return voucher;
		} catch (SQLException e) {
			if (DataDuplicationException.UNIQUE_KEY_ERROR_CODE == e
					.getErrorCode())
				throw new DataDuplicationException(e.getMessage());
			else
				rollbackTransaction();
			throw e;
		} finally {
			closeConnection();
		}
	}
	
	
	public SavingDataList findSaving(CriteriaRMParams cri)
			throws Exception {
		openConnection();
		try {
			savingDao = RemittanceDAOFactory.getSavingDAO(dbManager
					.getConnection());
			return savingDao.findSaving(cri);
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}
	
	public Saving getSaving(Saving saving) throws Exception {
		openConnection();
		try {
			savingDao = RemittanceDAOFactory.getSavingDAO(dbManager
					.getConnection());
			return savingDao.getSaving(saving);
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}
	
	public void delete(Saving saving,long userId, Date deletedDate) throws Exception {
		openConnection();
		try {
			savingDao = RemittanceDAOFactory.getSavingDAO(dbManager
					.getConnection());
			agentbalanceDAO = RemittanceDAOFactory.getAgentBalanceDAO(dbManager
					.getConnection());
			logDAO = CommonDAOFactory.getLogDAO(getConnection());
			Log l_logentity = new Log();
			l_logentity.setTablename("Saving");
			l_logentity.setLogkey(KeyGenerator.generateKey(getConnection()));
			l_logentity.setKey(saving.getSavingid());
			l_logentity.setUserid(userId);
			l_logentity.setLogstatus(Log.__DELETE_DATA);
			logDAO.getData(l_logentity);
			
			gotoGL(saving, GL_STATUS.DELETE);
			GoToAgentBalance(STATUS.DELETE, saving);			
			savingDao.delete(saving);
						
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}
	
	public void GoToAgentBalance(STATUS Status,Saving entity) throws Exception {
		try {
			agentbalanceDAO = RemittanceDAOFactory.getAgentBalanceDAO(dbManager
					.getConnection());
			AgentBalance l_agent = new AgentBalance();
			if(Status == STATUS.ADD){
				l_agent = new AgentBalance();
				l_agent.setAgent_id(entity.getAgentid());
				l_agent.setCurrencyid(entity.getCurrencyid());
				l_agent = agentbalanceDAO.getInfo(l_agent);
				if(l_agent == null)
				{
					l_agent = new AgentBalance();
					l_agent.setAgentbalance_id(KeyGenerator.generateKey(getConnection()));
					l_agent.setAgent_id(entity.getAgentid());
					l_agent.setCurrencyid(entity.getCurrencyid());
					if(entity.getTransactiontype() == 1)
						l_agent.setCurrentbalance(-entity.getAmount());
					else
						l_agent.setCurrentbalance(entity.getAmount());
					agentbalanceDAO.create(l_agent);
				}
				else
				{
					if(entity.getTransactiontype() == 1)
						l_agent.setCurrentbalance(l_agent.getCurrentbalance()-entity.getAmount());
					else
						l_agent.setCurrentbalance(l_agent.getCurrentbalance()+entity.getAmount());
					agentbalanceDAO.update(l_agent);
				}
			}
			else if(Status == STATUS.UPDATE)
			{
				l_agent = new AgentBalance();
				double amt = 0;
				double tempamt = 0;
				Saving tempentity = new Saving();
				tempentity = savingDao.getSaving(entity);
				amt = entity.getAmount();
				tempamt = tempentity.getAmount();
				long currencyid= entity.getCurrencyid();
				long tempcur = tempentity.getCurrencyid();
				long agent = entity.getAgentid();
				long tempagent = tempentity.getAgentid();
				
				if(tempcur == currencyid && tempagent == agent)
						{
							l_agent.setAgent_id(entity.getAgentid());
							l_agent.setCurrencyid(entity.getCurrencyid());	
							l_agent = agentbalanceDAO.getInfo(l_agent);

							if(entity.getTransactiontype() == 1)
								l_agent.setCurrentbalance((l_agent.getCurrentbalance()+tempamt) - amt);
							else
								l_agent.setCurrentbalance((l_agent.getCurrentbalance()-tempamt) + amt);
						}
						else
						{
							double amount = 0;
							l_agent.setCurrencyid(tempcur);
							l_agent.setAgent_id(tempagent);
							l_agent = agentbalanceDAO.getInfo(l_agent);
							if(entity.getTransactiontype() == 1)
								amount = l_agent.getCurrentbalance() + tempamt;
							else
								amount = l_agent.getCurrentbalance() - tempamt;
							
							l_agent.setCurrentbalance(amount);
							agentbalanceDAO.update(l_agent);			
							
							l_agent.setAgent_id(entity.getAgentid());
							l_agent.setCurrencyid(entity.getCurrencyid());	
							l_agent = agentbalanceDAO.getInfo(l_agent);
							if(l_agent == null)
							{
								l_agent = new AgentBalance();
								l_agent.setAgentbalance_id(KeyGenerator.generateKey(getConnection()));
								l_agent.setAgent_id(entity.getAgentid());
								l_agent.setCurrencyid(entity.getCurrencyid());
								l_agent.setOpeningbalance(0);
								if(entity.getTransactiontype() == 1)
									l_agent.setCurrentbalance(-amt);
								else
									l_agent.setCurrentbalance(amt);
					
								agentbalanceDAO.create(l_agent);
							}
							else
							{
								if(entity.getCurrencyid() == tempcur && entity.getAgentid() == tempagent){
									if(entity.getTransactiontype() == 1)
										l_agent.setCurrentbalance(amount-amt);
									else
										l_agent.setCurrentbalance(amount+amt);
								}
								else{
									if(entity.getTransactiontype() == 1)
										l_agent.setCurrentbalance(l_agent.getCurrentbalance() - amt);
									else
										l_agent.setCurrentbalance(l_agent.getCurrentbalance() + amt);
								}
							}
						}
						agentbalanceDAO.update(l_agent);
			}
			else
			{
				l_agent = new AgentBalance();
				l_agent.setCurrencyid(entity.getCurrencyid());
				l_agent.setAgent_id(entity.getAgentid());
				l_agent = agentbalanceDAO.getInfo(l_agent);
				if(l_agent != null)
				{
					l_agent.setAgent_id(entity.getAgentid());
					l_agent.setCurrencyid(entity.getCurrencyid());
					if(entity.getTransactiontype() == 1)
						l_agent.setCurrentbalance(l_agent.getCurrentbalance()+entity.getAmount());
					else
						l_agent.setCurrentbalance(l_agent.getCurrentbalance()-entity.getAmount());
					
					agentbalanceDAO.update(l_agent);
				}
			}
		} catch(Exception e) { throw e; }
		/* -- END GL ADD DATA -- */
	} 
	
	//goto GL
	public void gotoGL(Saving l_entity, GL_STATUS status) throws Exception {
	try{	
			currencyRateDAO = CommonDAOFactory.getCurrencyRateDAO(getConnection());
			generalCOADAO = RemittanceDAOFactory.getGeneral_COADAO(getConnection());		
			bankDAO = RemittanceDAOFactory.getBankDAO(getConnection());
			GeneralLedgerMgr l_glmgr = new GeneralLedgerMgr(this.sessionId);
			if(status == GL_STATUS.DELETE){
				if(l_entity.getTransactiontype() == 1)
					l_glmgr.deleteGL(l_entity.getSavingno()+ "-SV");
				else
					l_glmgr.deleteGL(l_entity.getSavingno() + "-SR");
			}
			
			if(l_entity.getTransactiontype() == 1)
				l_glmgr.deleteGL(l_entity.getSavingno()+ "-SV");
			else
				l_glmgr.deleteGL(l_entity.getSavingno() + "-SR");
			
			if(status == GL_STATUS.CREATE) {				
				General_COA l_generalCOA = generalCOADAO.find(l_entity.getBranchId());
				Bank l_Bank = new Bank();
				if(l_entity.getBankid() != null){
					l_Bank = bankDAO.findByCode(l_entity.getBankcode(), l_entity.getBranchId());
				}
				GeneralLedger _glHeader = new GeneralLedger();
				//_glHeader.setGlKey(KeyGenerator.generateKey(getConnection()));
				_glHeader.setGlKey(l_glmgr.getGLKey());
				if(l_entity.getTransactiontype() == 1)
					_glHeader.setSlip_ID(l_entity.getSavingno() + "-SV");
				else
					_glHeader.setSlip_ID(l_entity.getSavingno() + "-SR");
				_glHeader.setSlipDate(l_entity.getSavingdate());
				_glHeader.setSystemDate(new Date());
				_glHeader.setStatus("JH");
				_glHeader.setSrNo(1);
				_glHeader.setOffice_ID(l_entity.getBranchId());			

				CurrencyRate l_currencyrate = currencyRateDAO.getCurrencyRateFromSourcetoHome(l_entity.getCurrencyid(), l_entity.getBranchId());
				
				if(l_entity.getTransactiontype() == 1){
					GeneralLedger _glDebit = new GeneralLedger();
					//_glDebit.setGlKey(KeyGenerator.generateKey(getConnection()));
					_glDebit.setGlKey(l_glmgr.getGLKey());
					_glDebit.setSlip_ID(l_entity.getSavingno() + "-SV");
					_glDebit.setSlipDate(l_entity.getSavingdate());
					_glDebit.setSystemDate(new Date());
					if(l_entity.getBankid() == null){
						_glDebit.setCoaKey(l_generalCOA.getCashAccountID());
						_glDebit.setDepartmentKey(l_generalCOA.getCashDepartmentID());
					}else{
						_glDebit.setCoaKey(l_Bank.getCoaid());
						_glDebit.setDepartmentKey(l_Bank.getDepartmentid());
					}
					_glDebit.setCurrencyKey(l_entity.getCurrencyid());
					_glDebit.setCurrencyRateKey(l_currencyrate.getCurrencyRateId());
					_glDebit.setExchangeRate(l_currencyrate.getRate());
					_glDebit.setSourceAmount(l_entity.getAmount());
					
					if(l_currencyrate.getRoper().equals("M"))
						_glDebit.setHomeAmount((l_entity.getAmount()) * l_currencyrate.getRate());
					else
						_glDebit.setHomeAmount((l_entity.getAmount() / l_currencyrate.getRate()));
					
					_glDebit.setStatus("JSV");	
					_glDebit.setSrNo(1);
					_glDebit.setOffice_ID(l_entity.getBranchId());			
					l_glmgr.createGL(_glDebit);		
					
					GeneralLedger _glCredit = new GeneralLedger();
					//_glCredit.setGlKey(KeyGenerator.generateKey(getConnection()));
					_glCredit.setGlKey(l_glmgr.getGLKey());
					_glCredit.setSlip_ID(l_entity.getSavingno() + "-SV");
					_glCredit.setSlipDate(l_entity.getSavingdate());
					_glCredit.setSystemDate(new Date());
					_glCredit.setCoaKey(l_generalCOA.getLiabilitiesAccountID());
					_glCredit.setDepartmentKey(l_generalCOA.getLiabilitiesDepartmentID());			
					_glCredit.setCurrencyKey(l_entity.getCurrencyid());
					_glCredit.setCurrencyRateKey(l_currencyrate.getCurrencyRateId());
					_glCredit.setExchangeRate(l_currencyrate.getRate());
					_glCredit.setSourceAmount(-(l_entity.getAmount()));
					
					if(l_currencyrate.getRoper().equals("M"))
						_glCredit.setHomeAmount(-(l_entity.getAmount() * l_currencyrate.getRate()));
					else
						_glCredit.setHomeAmount(-(l_entity.getAmount() / l_currencyrate.getRate()));
					
					_glCredit.setStatus("JSV");
					_glCredit.setSrNo(2);
					_glCredit.setOffice_ID(l_entity.getBranchId());
					
					l_glmgr.createGL(_glCredit);
				}else{
					GeneralLedger _glDebit = new GeneralLedger();
					//_glDebit.setGlKey(KeyGenerator.generateKey(getConnection()));
					_glDebit.setGlKey(l_glmgr.getGLKey());
					_glDebit.setSlip_ID(l_entity.getSavingno() + "-SR");
					_glDebit.setSlipDate(l_entity.getSavingdate());
					_glDebit.setSystemDate(new Date());
					_glDebit.setCoaKey(l_generalCOA.getLiabilitiesAccountID());
					_glDebit.setDepartmentKey(l_generalCOA.getLiabilitiesDepartmentID());	
					_glDebit.setCurrencyKey(l_entity.getCurrencyid());
					_glDebit.setCurrencyRateKey(l_currencyrate.getCurrencyRateId());
					_glDebit.setExchangeRate(l_currencyrate.getRate());
					_glDebit.setSourceAmount(l_entity.getAmount());
					
					if(l_currencyrate.getRoper().equals("M"))
						_glDebit.setHomeAmount((l_entity.getAmount()) * l_currencyrate.getRate());
					else
						_glDebit.setHomeAmount((l_entity.getAmount() / l_currencyrate.getRate()));
					
					_glDebit.setStatus("JSR");	
					_glDebit.setSrNo(1);
					_glDebit.setOffice_ID(l_entity.getBranchId());			
					l_glmgr.createGL(_glDebit);		
					
					GeneralLedger _glCredit = new GeneralLedger();
					//_glCredit.setGlKey(KeyGenerator.generateKey(getConnection()));
					_glCredit.setGlKey(l_glmgr.getGLKey());
					_glCredit.setSlip_ID(l_entity.getSavingno() + "-SR");
					_glCredit.setSlipDate(l_entity.getSavingdate());
					_glCredit.setSystemDate(new Date());
					if(l_entity.getBankid() == null){
						_glCredit.setCoaKey(l_generalCOA.getCashAccountID());
						_glCredit.setDepartmentKey(l_generalCOA.getCashDepartmentID());
					}else{
						_glCredit.setCoaKey(l_Bank.getCoaid());
						_glCredit.setDepartmentKey(l_Bank.getDepartmentid());
					}				
					_glCredit.setCurrencyKey(l_entity.getCurrencyid());
					_glCredit.setCurrencyRateKey(l_currencyrate.getCurrencyRateId());
					_glCredit.setExchangeRate(l_currencyrate.getRate());
					_glCredit.setSourceAmount(-(l_entity.getAmount()));
					
					if(l_currencyrate.getRoper().equals("M"))
						_glCredit.setHomeAmount(-(l_entity.getAmount() * l_currencyrate.getRate()));
					else
						_glCredit.setHomeAmount(-(l_entity.getAmount() / l_currencyrate.getRate()));
					
					_glCredit.setStatus("JSR");
					_glCredit.setSrNo(2);
					_glCredit.setOffice_ID(l_entity.getBranchId());
					
					l_glmgr.createGL(_glCredit);
				}
				l_glmgr.createGL(_glHeader);
			}
		} catch(Exception e) { throw e; }
	}

	//Report
	public JasperPrint prepareSavingReport(SavingRpt info)
			throws Exception {
		openConnection();
		JasperPrint l_jsPrint = null;
		try {
			savingDao = RemittanceDAOFactory.getSavingDAO(dbManager.getConnection());
			JRResultSetDataSource l_jrRS = new JRResultSetDataSource(savingDao.prepareSavingReport(info));
			JasperReport l_jsRpt = null;
			if(info.getOrderby().equals("currencyid"))
				l_jsRpt = (JasperReport) JRLoader
				.loadObjectFromFile(CommonSingleton.ABSOLUTE_PATH +"reports/SavingByCurrencyRpt.jasper");
			else
				l_jsRpt = (JasperReport) JRLoader
					.loadObjectFromFile(CommonSingleton.ABSOLUTE_PATH +"reports/SavingRpt.jasper");
	
			HashMap<String, Object> l_params = new HashMap<String, Object>();
			l_params.put("companyname", "mbc");
			l_jsPrint = JasperFillManager.fillReport(l_jsRpt, l_params, l_jrRS);

		} catch (JRException je) {
			throw je;
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}

		return l_jsPrint;

	}
	
	public JasperPrint prepareSavingBalanceReport(SavingBalanceRpt info)
			throws Exception {
		openConnection();
		JasperPrint l_jsPrint = null;
		try {
			savingDao = RemittanceDAOFactory.getSavingDAO(dbManager.getConnection());
			JRResultSetDataSource l_jrRS = new JRResultSetDataSource(savingDao.prepareSavingBalanceReport(info));
			JasperReport l_jsRpt = null;
			if(info.getGroupby().equals("CurrencyID"))
				l_jsRpt = (JasperReport) JRLoader
				.loadObjectFromFile(CommonSingleton.ABSOLUTE_PATH +"reports/SavingBalanceforCurRpt.jasper");
			else
				l_jsRpt = (JasperReport) JRLoader
					.loadObjectFromFile(CommonSingleton.ABSOLUTE_PATH +"reports/SavingBalanceRpt.jasper");
	
			HashMap<String, Object> l_params = new HashMap<String, Object>();
			l_params.put("companyname", "mbc");
			l_jsPrint = JasperFillManager.fillReport(l_jsRpt, l_params, l_jrRS);

		} catch (JRException je) {
			throw je;
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}

		return l_jsPrint;

	}
	
	public JasperPrint prepareSavingLedgerReport(SavingLedgerRpt info)
			throws Exception {
		openConnection();
		JasperPrint l_jsPrint = null;
		try {
			savingDao = RemittanceDAOFactory.getSavingDAO(dbManager.getConnection());
			JRResultSetDataSource l_jrRS = new JRResultSetDataSource(savingDao.prepareSavingLedgerReport(info));
			JasperReport l_jsRpt = null;
			
			l_jsRpt = (JasperReport) JRLoader
			.loadObjectFromFile(CommonSingleton.ABSOLUTE_PATH +"reports/SavingLedgerRpt.jasper");
		
			HashMap<String, Object> l_params = new HashMap<String, Object>();
			l_params.put("companyname", "mbc");
			l_jsPrint = JasperFillManager.fillReport(l_jsRpt, l_params, l_jrRS);

		} catch (JRException je) {
			throw je;
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}

		return l_jsPrint;

	}
}
